package com.huazai.out_school_manager.Util;

import java.sql.*;
import java.util.ResourceBundle;

/**
 * @author EZTTU
 * 数据库链接的工具类
 */
public class DBUtil {
    private static final ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
    private static final String URL = bundle.getString("url");
    private static final String user = bundle.getString("user");
    private static final String password = bundle.getString("password");
    private static final String driver = bundle.getString("driver");


    static {
        try {
            Class.forName(driver);
//            System.out.println("Driver loading succeed !");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
//            System.out.println("Driver loading failed !");
        }
    }

    public static Connection getConnectDB() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(URL,user,password);
//            System.out.println("Connect to database succeed !");
        } catch (SQLException e) {
            e.printStackTrace();
//            System.out.println("Connect to database failed !");
        }
        return conn;
    }

    public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
